package com.example.wacoffee_user.controller;


import com.example.wacoffee_user.pojo.dto.OrderPayDTO;
import com.example.wacoffee_user.pojo.entity.Order;
import com.example.wacoffee_user.pojo.entity.item;
import com.example.wacoffee_user.service.IOrderService;
import com.example.wacoffee_user.utils.CurrentHoler;
import com.example.wacoffee_user.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.jaxb.SpringDataJaxb;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/order")
public class orderController {

    @Autowired
    private IOrderService orderService;
    @PostMapping
    public Result addOrder(@RequestBody Order order){
        item[] items = order.getItems();
        double sum=0;
        for(item i:items){
            sum+=i.getPrice()*i.getQuantity();
        }
        if(sum<=(order.getExpend()-1))return Result.error("支付异常");
        return Result.success(orderService.addOrder(order));
    }

    @GetMapping("/all")
    public Result getAllOrder(){
        List<Order>list=orderService.getAllOrder();
        return Result.success(list);
    }

    @GetMapping("/noPaid")
    public Result getNoPaidOrder(){
        List<Order>list=orderService.getNoPaidOrder();
        return Result.success(list);
    }

    @GetMapping("/processed")
    public Result getProcessedOrder(){
        List<Order>list=orderService.getProcessedOrder();
        return Result.success(list);
    }

    @GetMapping("/canceled")
    public Result getCanceledOrder(){
        List<Order>list=orderService.getCanceledOrder();
        return Result.success(list);
    }

    @PutMapping("/pay")
    public Result payOrder(@RequestBody OrderPayDTO order){
        order.setUserId(CurrentHoler.getCurrentId());
        orderService.payOrder(order);
        return Result.success();
    }

    @PutMapping("/cancelPay")
    public Result cancelPay(@RequestParam int id){
        orderService.cancelPay(id);
        return Result.success();
    }

}
